using FireStarter.Base;
using FireStarterModeller.BO;
using Habanero.BO;
using NUnit.Framework;

namespace FireStarterModeller.Test.BO
{
    
    /// <summary>
    /// Provides a place to write custom tests for DMLookupList objects.
    /// This file is only written once and can be changed.  The Def file
    /// attached to this as a dependent is rewritten with each regeneration
    /// and contains the standard tests for DMLookupList.
    /// Regenerate this test project whenever there have been changes to the
    /// business objects.
    /// If tests are failing due to a unique setup in your application,
    /// you can either override the Create methods in TestUtils, or you
    /// can add the test to the ignore list below and reimplement it here.
    /// </summary>
    public abstract partial class TestDMLookupList
    {
        protected abstract DMLookupList GetSavedLookupList();
        protected abstract DMLookupList GetUnsavedLookupList();

        [TestFixture]
        public class TestSimpleLookupListInfo : TestDMLookupList
        {
            protected override DMLookupList GetSavedLookupList()
            {
                return TestUtilsShared.CreateSavedSimpleLookupListInfo();
            }

            protected override DMLookupList GetUnsavedLookupList()
            {
                return TestUtilsShared.CreateUnsavedValidSimpleLookupListInfo();
            }
            [Test]
            public void Test_ToString()
            {
                //---------------Set up test pack-------------------
                DMLookupList dmLookupList = TestUtilsDMLookupList.CreateUnsavedValidDMLookupList();
                //---------------Assert Precondition----------------
                Assert.IsNotNull(dmLookupList.LookupListID);
                //---------------Execute Test ----------------------
                string actualToString = dmLookupList.ToString();
                //---------------Test Result -----------------------
                Assert.AreEqual(dmLookupList.LookupListID.ToString(), actualToString);
            }

            [Test]
            public void Test_ToString_LookupListIsNull_IDIsNull()
            {
                //---------------Set up test pack-------------------
                DMLookupList dmLookupList = TestUtilsDMLookupList.CreateUnsavedValidDMLookupList();
                dmLookupList.LookupListID = null;
                //---------------Assert Precondition----------------
                Assert.IsNull(dmLookupList.LookupListID);
                //---------------Execute Test ----------------------
                string actualToString = dmLookupList.ToString();
                //---------------Test Result -----------------------
                Assert.AreEqual("-unnamed DMLookupList-", actualToString);
            }
            [Test]
            public void Test_UpdateSimpleLookupListInfoS()
            {
                //---------------Set up test pack-------------------
                DMLookupListSimple dmLookupListSimple = TestUtilsShared.CreateSavedSimpleLookupListInfo();

                //---------------Assert Precondition----------------
                Assert.IsFalse(dmLookupListSimple.Status.IsNew);
                string message;
                Assert.IsTrue(dmLookupListSimple.IsEditable(out message));
                Assert.AreEqual(1, dmLookupListSimple.ListItems.Count);

                //---------------Execute Test ----------------------
                UpdateItemsInSimpleLookupListInfoWithTwoItems(dmLookupListSimple);

                //---------------Test Result -----------------------
                Assert.IsFalse(dmLookupListSimple.Status.IsNew);
                Assert.AreEqual(3, dmLookupListSimple.ListItems.Count);
            }

            private static void UpdateItemsInSimpleLookupListInfoWithTwoItems(DMLookupListSimple dmLookupListSimple)
            {
                DMListItem dmListItemInfo1 = TestUtilsShared.CreateUnsavedValidItemInfo(dmLookupListSimple);
                dmListItemInfo1.Save();
                DMListItem dmListItemInfo2 = TestUtilsShared.CreateUnsavedValidItemInfo(dmLookupListSimple);
                dmListItemInfo2.Save();
            }
        }

        [TestFixture]
        public class TestDMLookupListBusinessObject : TestDMLookupList
        {
            protected override DMLookupList GetSavedLookupList()
            {
                DMLookupListBusinessObject lookupListBusinessObject = (DMLookupListBusinessObject) GetUnsavedLookupList();
                lookupListBusinessObject.Save();
                return lookupListBusinessObject;
            }

            [TestFixtureSetUp]
            public void SetupTestFixture()
            {
                //------------------------------------------------------------
                // Use this list to ignore generated tests that are failing
                // due to a unique condition in your application.
                // Remember to reimplement the test here.
                //------------------------------------------------------------
                //_ignoreList.Add("TestMethodName", "Reason for ignoring it");
                base.SetupFixture();
            }

            protected override DMLookupList GetUnsavedLookupList()
            {
                DMLookupListBusinessObject dmObjectLookupListBusinessObject = new DMLookupListBusinessObject();
                dmObjectLookupListBusinessObject.DMClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
                dmObjectLookupListBusinessObject.AssemblyName = TestUtilsShared.GetRandomString();
                return dmObjectLookupListBusinessObject;
            }

            [Test]
            public void Test_InsertNewBusinessObjectLookupListInfo_Invalid()
            {
                //---------------Set up test pack-------------------
                DMLookupListBusinessObject dmObjectLookupListBusinessObject = new DMLookupListBusinessObject();
                //---------------Assert Precondition----------------
                Assert.IsTrue(dmObjectLookupListBusinessObject.Status.IsNew);
                //---------------Execute Test ----------------------
                try
                {
                    dmObjectLookupListBusinessObject.Save();
                    Assert.Fail("expected Err");
                }
                //---------------Test Result -----------------------
                catch (BusObjectInAnInvalidStateException ex)
                {
                    StringAssert.Contains("Class ID' is a compulsory field and has no value", ex.Message);
                }
                Assert.IsTrue(dmObjectLookupListBusinessObject.Status.IsNew);
            }
        }
        
        [TestFixture]
        public class TestDatabaseLookupListInfo : TestDMLookupList
        {
            protected override DMLookupList GetSavedLookupList()
            {
                return CreateSavedValidDatabaseLookupListInfo();
            }

            private DMLookupList CreateSavedValidDatabaseLookupListInfo()
            {
                DMLookupListDatabase lookupListDatabase = (DMLookupListDatabase) GetUnsavedLookupList();
                lookupListDatabase.Save();
                return lookupListDatabase;
            }

            protected override DMLookupList GetUnsavedLookupList()
            {
                DMLookupListDatabase lookupListDatabase = new DMLookupListDatabase
                      {
                          Sql = TestUtilsShared.GetRandomString(),
                          LookupClassName = TestUtilsShared.GetRandomString(),
                          Assembly = TestUtilsShared.GetRandomString()
                      };
                return lookupListDatabase;
            }

            [Test]
            public void Test_InsertNewDatabaseLookupListInfo_Invalid()
            {
                //---------------Set up test pack-------------------
                DMLookupListDatabase dmLookupListDatabase = new DMLookupListDatabase();

                //---------------Assert Precondition----------------
                Assert.IsTrue(dmLookupListDatabase.Status.IsNew);

                //---------------Execute Test ----------------------
                try
                {
                    dmLookupListDatabase.Save();
                    Assert.Fail("expected Err");
                }
                //---------------Test Result -----------------------
                catch (BusObjectInAnInvalidStateException ex)
                {
                    StringAssert.Contains("Assembly' is a compulsory field and has no value", ex.Message);
                    StringAssert.Contains("Class Name' is a compulsory field and has no value", ex.Message);
                    StringAssert.Contains("Sql' is a compulsory field and has no value", ex.Message);
                }
                Assert.IsTrue(dmLookupListDatabase.Status.IsNew);
            }
        }

        [SetUp]
        public override void SetupTest()
        {
            //Runs every time that any testmethod is executed
            base.SetupTest();
        }

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            //Code that is executed before any test is run in this class. If multiple tests
            // are executed then it will still only be called once.\
            base.SetupFixture();
        }

        [TearDown]
        public override void TearDownTest()
        {
            //runs every time any testmethod is complete
            base.TearDownTest();
        }

        [Test]
        public void Test_InsertNewLookupListInfo()
        {
            //---------------Set up test pack-------------------
            DMLookupList dmLookupList = GetUnsavedLookupList();

            //---------------Assert Precondition----------------
            Assert.IsTrue(dmLookupList.Status.IsNew);

            //---------------Execute Test ----------------------
            dmLookupList.Save();

            //---------------Test Result -----------------------
            Assert.IsFalse(dmLookupList.Status.IsNew);
        }

        [Test]
        public void Test_DeleteLookupListInfo()
        {
            //---------------Set up test pack-------------------
            DMLookupList dmLookupList = GetSavedLookupList();

            //---------------Assert Precondition----------------
            Assert.IsFalse(dmLookupList.Status.IsNew);
            string message;
            Assert.IsTrue(dmLookupList.IsDeletable(out message));

            //---------------Execute Test ----------------------
            dmLookupList.MarkForDelete();
            dmLookupList.Save();

            //---------------Test Result -----------------------  
            Assert.IsTrue(dmLookupList.Status.IsNew);
            Assert.IsTrue(dmLookupList.Status.IsDeleted);
        }

        [Test]
        public void Test_IModelElement_Parent_ShouldReturnProperty()
        {
            //---------------Set up test pack-------------------
            DMLookupList dmLookupList = GetUnsavedLookupList();
            IModelElement modelElement = (IModelElement)dmLookupList;
            DMProperty property = new DMProperty();
            dmLookupList.DMProperty = property;
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmLookupList.DMProperty);
            //---------------Execute Test ----------------------
            IModelElement parent = modelElement.Parent;
            //---------------Test Result -----------------------
            Assert.AreSame(property, parent);
        }
        
    }
}